#ifndef _FRACTAL_FVERTEXARRAY_TYPES_H_
#define _FRACTAL_FVERTEXARRAY_TYPES_H_


#include "fvector.hpp"
#include <vector>

using std::vector;


template <class VectorType>
class FVertexArray
{
    typedef typename VectorType::coord_type coord_type;

public:
    FVertexArray(int size=0, const coord_type &val=coord_type())
    {
	_capacity = _size = 0;
	this->resize(size, val);
    }

    FVertexArray(int size, const VectorType &val)
    {
	_capacity = _size = 0;
	this->resize(size, val);
    }

    FVertexArray(coord_type *coords, int size):
	_buf(coords, size)
    { 
	_capacity = _size = size;
    }

    FVertexArray(const FVertexArray<VectorType> &that):
	_buf(that._buf)
    {
	_capacity = that._capacity;
	_size = that._size;
    }

    FVertexArray<VectorType> &
    operator=(const FVertexArray<VectorType> &that)
    {
	if (this == &that)
	    return *this;
	_buf = that._buf;
	_capacity = that._capacity;
	_size = that._size;
	return *this;
    }

    coord_type &operator[](int i)
    {
	return _buf[i];
    }

    int size(void) const
    {
	return _size;
    }

    void resize(int n, const VectorType &val=VectorType())
    {
	int new_cnum = n * VectorType::dim;
	_buf.resize(new_cnum);
	for (int i = _size; i < n; i++) {
	    int pos = i * VectorType::dim;
	    for (int j = 0; j < VectorType::dim; j++)
		_buf[pos+j] = val.get_coord(j);
	}
	_size = _capacity = n;
    }

    void resize(int n, coord_type val) 
    {
	this->resize(n, VectorType(val));
    }

    int capacity(void) const
    {
	return _capacity;
    }

    void reserve(int n)
    {
	int new_cnum = n * VectorType::dim;
	_buf.reserve(new_cnum); _buf.resize(0);
	_capacity = n;
	_size = 0;
    }

    int coord_size(void) const
    {
	return _buf.size();
    }

    void push_back(const VectorType &v)
    {
	if (_size == _capacity)
	    this->resize(_size+1, v);
	else {
	    for (int i = 0; i < VectorType::dim; i++)
		_buf.push_back(v.get_coord(i));
	    _size++;
	}
    }

    const coord_type *c_ptr(void) const
    {
	return &_buf[0];
    }

private:
    vector<coord_type> _buf;
    int _size, _capacity;
};


#endif  // _FRACTAL_FVERTEXARRAY_TYPES_H_
